$(function() {
	// $('#delModal').modal('hide');
	topicInfo();
	$(".uploadBtn").click(function() {
		$("#file").click();
	});
});
var token;
var hash = window.location.hash.slice(1);
var id = hash.split(",")[0];
var groupId = hash.split(",")[1];
var queryOrgId = hash.split(",")[2];
var type = hash.split(",")[3];
// 列表查询
function topicInfo() {
	$.get('../../../hivelearn/topic/findTopic.json?id=' + id
							+ '&groupId=' + groupId,
					function(data) {
						if (data.success) {
							$("#topicTitle").val(data.data.topicTitle);
							if(data.data.topicCover){
								$("#topicCover").attr("src","../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
										+ data.data.topicCover);
							}
							if (data.data.cards.length > 0) {
								$(".hasCard").text("单击并按住拖动卡片进行排序。");
							}
							var dom = _.template($('#reorderTpl').html());
							dom = dom(data.data);
							$(".reorder").html(dom);
							/*
							 * $('.reorder').gridly({ //base : 60, // px //
							 * gutter : 20, // px columns : 1, callbacks : {
							 * reordering : reordering, reordered : reordered }
							 * });
							 */
							token = data.tokenmessage.token;
							Sortable.create($(".reorder")[0], {
								group : "omega"
							});
						}
					})
}

function updateTopic() {
	var topic = new Object();
	topic.topicId = id;
	topic.topicTitle = $("#topicTitle").val()?$("#topicTitle").val():"未定义";
	if($("[name='topicCover']").val()){
		topic.topicCover = $("[name='topicCover']").val();
	}
	var arr = [];
	$(".reorder").find("li").each(function(e) {
		var obj = new Object();
		obj.cardId = $(this).attr("id");
		obj.top = e;
		arr.push(obj);
	});
	topic.cards = arr;
	$.ajax({
		url : '../../../hivelearn/topic/updateTopic.json',
		data : {
			"topicInfo" : JSON.stringify(topic),
			"groupId" : groupId,
			"ORCHID_WEB_TOKEN":token
		},
		dataType : "json",
		success : function(data) {
			if(type==1){
				window.location.href="../../../hivelearnweb/portal/meetingtheme/meetingTheme.html#"+groupId+","+queryOrgId;
			}else{
				window.location.href="../../../hivelearnweb/portal/studyTalk/studyTalk.html#"+id+","+groupId+","+queryOrgId;
			}
		}
	})
}
function toList(){
	if(type==1){
		window.location.href="../../../hivelearnweb/portal/meetingtheme/meetingTheme.html#"+groupId+","+queryOrgId;
	}else{
		window.location.href="../../../hivelearnweb/portal/studyTalk/studyTalk.html#"+id+","+groupId+","+queryOrgId;
	}
}
function uploadFile(e) {
	/*if ($("#file").val()&&checkfile(e)) {
		$.ajax({
			url : "../../../hivelearn/topic/uploadImage.json",
			data : new FormData($('#uploadForm')[0]),
			method : "post",
			processData : false,
			contentType : false,
			cache : false,
			success : function(data) // 服务器成功响应处理函数
			{
				$("#topicCover").attr(
						"src",
						"../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
								+ data.data.attachid);
				$('[name="topicCover"]').val(data.data.attachid);
				console.log("上传成功");
				$("#file").val("");

			},
			error : function(data)// 服务器响应失败处理函数
			{
				console.log("服务器异常");
				$("#file").val("");
			}
		});
	}

	return false;*/
	$("#uploadForm").ajaxSubmit({
    	url: "../../../hivelearn/topic/uploadImage.json?_t="+new Date().getTime(),
        data:$("#uploadForm").serialize(),
        dataType:"json",
        success:function(data){
        	$("#topicCover").attr(
					"src",
					"../../../components/gtattachment/open/downloadAttachment.json?attachmentId="
							+ data.attachid);
			$('[name="topicCover"]').val(data.attachid);
			console.log("上传成功");
			$("#file").val("");
		},
		error:function(data){
			console.log("服务器异常");
			$("#file").val("");
		}
    });
}
//检查
/*function checkfile(target) {
	var isIE = /msie/i.test(navigator.userAgent) && !window.opera; 
	var fileSize = 0;
	var filetypes = [ ".jpg", ".png", ".gif"];
	var filepath = target.value;
	var filemaxsize = 1024 * 2;// 2M
	if (filepath) {
		var isnext = false;
		var fileend = filepath.substring(filepath.indexOf("."));
		if (filetypes && filetypes.length > 0) {
			for ( var i = 0; i < filetypes.length; i++) {
				if (filetypes[i] == fileend) {
					isnext = true;
					break;
				}
			}
		}
		if (!isnext) {
			$("#msgModal").find(".msg").text("请选择.png，.jpg，.gif图片文件上传");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
	} else {
		return false;
	}
	if (isIE && !target.files) {
		ieUpload(target,"uploadForm",uploadFile,filemaxsize);
	} else {
		fileSize = target.files[0].size;
		var size = fileSize / 1024;
		if (size > filemaxsize) {
			$("#msgModal").find(".msg").text("图片大小不能大于" + filemaxsize / 1024 + "M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		if (size <= 0) {
			$("#msgModal").find(".msg").text("图片大小不能为0M！");
			$("#msgModal").modal("show");
			target.value = "";
			return false;
		}
		uploadFile();
	}
}*/